package com.atguigu.gulimall.product.controller;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrRelationDVo;
import com.atguigu.gulimall.product.vo.WithAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author Venttt
 * @email venttt@qq.com
 * @date 2022-04-12 15:27:27
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable(value = "catelogId", name = "catelogId") Long catelogId) {
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }

    /**
     * 根据分组id查询属性信息
     * @param attrGroupId
     * @return
     */
    @RequestMapping("{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable(value = "attrGroupId", name = "attrGroupId") Long attrGroupId) {
        List<AttrEntity> attrEntityList = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data", attrEntityList);
    }

    /**
     * 查询出没有关联的属性
     * @param attrGroupId
     * @return
     */
    //product/attrgroup/225/noattr/relation
    @RequestMapping("{attrGroupId}/noattr/relation")
    public R noAttrRelation(@RequestParam Map<String, Object> params,@PathVariable(value = "attrGroupId", name = "attrGroupId") Long attrGroupId) {
        PageUtils pageUtils = attrService.queryAttrPage(params, attrGroupId);
        return R.ok().put("page", pageUtils);
    }

    //获取分类下所有分组&关联属性
    ///product/attrgroup/{catelogId}/withattr
    @RequestMapping("{catelogId}/withattr")
    public R withAttr(@PathVariable(value = "catelogId", name = "catelogId") Long catelogId){
        List<WithAttrVo> withAttrList = attrGroupService.withAttrList(catelogId);
        return R.ok().put("data",withAttrList);
    }



    /**
     * 根据attrid和groupdid 关联关系表
     * @param vos
     * @return
     */
    @RequestMapping("attr/relation/delete")
    public R attrRelationDelete(@RequestBody AttrRelationDVo[] vos) {
        attrAttrgroupRelationService.deleteBatchByIds(vos);
        return R.ok().put("data","删除成功");
    }

    /**
     * 添加属性和分组的关联关系
     * @return
     */
    @RequestMapping("attr/relation")
    public R attrReleationSave(@RequestBody List<AttrRelationDVo> vos){

        attrAttrgroupRelationService.saveBatch(vos);
        return R.ok().put("data","");
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCategoryPath(catelogId);
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
